package com.jie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jie.entity.Shop;
import com.jie.service.ShopService;
import com.jie.utility.JwtUtils;
import com.jie.utility.R;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jie
 * @since 2024-02-24
 */
@RestController
@RequestMapping("/shop")
public class ShopController {
   @Autowired
   private ShopService shopService;
   @GetMapping("/shopR")
   public R<List<Shop>> shopR(HttpServletRequest request,@RequestParam(name = "createAdmin",required = false) Integer createAdmin){
      LambdaQueryWrapper<Shop> queryWrapper = new LambdaQueryWrapper<>();
      if(createAdmin!=null){
         queryWrapper.eq(Shop::getCreateAdmin,createAdmin);
      }else {
         String jwt = request.getHeader("Authorization");
         R r = JwtUtils.parseJwt(jwt);
         Map map = r.getMap();
         Integer type = (Integer) map.get("type");
         Integer createAdminId = (Integer) map.get("createAdminId");
         Integer adminId = (Integer) map.get("adminId");
         if(type==3){
            queryWrapper.eq(Shop::getCreateAdmin,createAdminId);
         }else {
            queryWrapper.eq(Shop::getCreateAdmin,adminId);
         }
      }
      Shop one = shopService.getOne(queryWrapper);
      List<Shop> list=new ArrayList<>();
      if(one==null){
     return R.error("没有数据");
      }
      list.add(one);
      return R.success(list);
   }
   @PostMapping("/addShop")
   public R<String> add(@RequestBody Shop shop,HttpServletRequest request){
      String jwt = request.getHeader("Authorization");
      R r = JwtUtils.parseJwt(jwt);
      Map map = r.getMap();
      Integer type = (Integer) map.get("type");
      Integer createAdminId = (Integer) map.get("createAdminId");
      Integer adminId = (Integer) map.get("adminId");
      if(type==3){
         shop.setCreateAdmin(createAdminId);
      }else {
         shop.setCreateAdmin(adminId);
      }
      boolean save = shopService.save(shop);
      if(save){
         return R.success("添加成功！");
      }
      else return R.error("添加失败！");
   }
   @PutMapping("/editShop")
   public R<String> edit(@RequestBody Shop shop){

      boolean update = shopService.updateById(shop);
      if(update) return R.success("修改成功！");
      return R.error("修改失败！");
   }
   @DeleteMapping("/delShop")
   public R<String> del(Shop shop){
      if(StringUtils.isNotEmpty(shop.getShopId())){
         boolean b = shopService.removeById(shop.getShopId());
         if(b){
            return R.success("删除成功！");
         }
        else {
            return R.error("删除失败！");
         }
      }
      return R.error("删除失败！");

   }
}

